/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * 
 */
public class ConstraintBodySection extends PropertyViewSection {

	/**
	 * 
	 */
	private Text body = null;
	
	/**
	 * 
	 */
	private DirectEditFocusListener directEditFocusListener= new DirectEditFocusListener();

	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory()
		.createFlatFormComposite(parent);
		FormData data;

		body = getWidgetFactory().createText(composite, "", SWT.V_SCROLL); 
		body.setEditable(true);
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(0, 100);
		body.setLayoutData(data);
		body.addFocusListener(directEditFocusListener);


		CLabel overviewLabel = getWidgetFactory().createCLabel(composite, "Specification:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(body,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(body, 0, SWT.TOP);
		overviewLabel.setLayoutData(data);
	}



	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {
		if (!body.isDisposed()){
			if (getElement() instanceof Constraint) {
				if(((Constraint)getElement()).getSpecification()!=null){
					body.removeFocusListener(directEditFocusListener);
					if(((Constraint)getElement()).getSpecification().stringValue()!=null){
					body.setText(((Constraint)getElement()).getSpecification().stringValue());}
					body.addFocusListener(directEditFocusListener);
				}
			}
			
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(body!=null && !body.isDisposed())
			body.removeFocusListener(directEditFocusListener);
	}
	
	/**
	 * Listener for a direct edit manager.
	 * 
	 * @author Remi SCHNEKENBURGER
	 */
	protected class DirectEditFocusListener implements org.eclipse.swt.events.FocusListener {
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void focusGained(FocusEvent e){
		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void focusLost(FocusEvent e){
			if((getElement() != null) && (getElement() instanceof Constraint)) {
				LiteralString constraintBody = UMLFactory.eINSTANCE.createLiteralString();
				constraintBody.setValue(body.getText().trim());
				Constraint constraint = (Constraint) getElement();
				constraintBody.setName("constraintBody");

				if (constraint.getSpecification() == null) {
					constraint.createSpecification("specification", null, UMLFactory.eINSTANCE.getUMLPackage().getLiteralString());
					constraint.setSpecification(constraintBody);		
				} else {
					constraint.setSpecification(constraintBody);	
				}
			}

		}
	};
	
	/**
	 * 
	 * 
	 * @param part 
	 * @param selection 
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);

	}
}

